#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <map>
#include <functional>
#include <cmath>
#include <cstring>

using namespace std;

class Solution
{
    typedef struct Node
    {
        set<int> hh;
    } Node;

public:
    vector<int> processQueries(int c, vector<vector<int>> &connections, vector<vector<int>> &queries)
    {
        vector<Node *> cc(c + 1, nullptr);
        vector<unordered_set<int>> graph(c + 1);
        for (auto connection : connections)
        {
            int u = connection[0], v = connection[1];
            graph[u].insert(v);
            graph[v].insert(u);
        }
        // dfs
        unordered_set<int> notVisited;
        for (int i = 1; i <= c; ++i)
        {
            notVisited.insert(i);
        }
        while (!notVisited.empty())
        {
            int start = *notVisited.begin();
            notVisited.erase(notVisited.begin());
            deque<int> dq;
            Node *nodesSet = new Node();
            dq.push_back(start);
            while (!dq.empty())
            {
                int node = dq.back();
                dq.pop_back();
                nodesSet->hh.insert(node);
                cc[node] = nodesSet;
                for (auto nn : graph[node])
                {
                    if (notVisited.count(nn))
                    {
                        notVisited.erase(nn);
                            dq.push_back(nn);
                    }
                }
            }
        }
        vector<int> ans;
        for (auto qu : queries)
        {
            int qtype = qu[0], node = qu[1];
            if (qtype == 2)
            {
                if (!cc[node] || cc[node]->hh.count(node) == 0)
                    continue;
                cc[node]->hh.erase(node);
            }
            else
            {
                if (!cc[node] || cc[node]->hh.empty())
                    ans.push_back(-1);
                else if(cc[node]->hh.count(node)){
                    ans.push_back(node);
                }
                else
                    ans.push_back(*cc[node]->hh.begin());
            }
        }
        return ans;
    }
};

    int
    main()
{

    // system("pause");
    return 0;
}
